<template>

  <div class="login-container">
    <div style="margin: 80px auto;">
      <el-radio-group v-model="loginModel">
        <el-radio-button label="staff">教职工登录</el-radio-button>
        <el-radio-button label="admin">管理员登录</el-radio-button>
      </el-radio-group>
      <div style="margin: 20px;"></div>
      <el-form ref="loginForm" :model="loginForm" label-width="80px">
        <el-form-item label="工号" v-if="loginModel === 'staff'">
          <el-input v-model="loginForm.account" placeholder="请输入工号"></el-input>
        </el-form-item>
        <el-form-item label="用户名" v-if="loginModel === 'admin'">
          <el-input v-model="loginForm.account" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" @click="handleLogin">登录</el-button>
          <el-button type="text" @click="openDialog">忘记密码</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog title="重置密码" :visible.sync="dialogVisible" width="30%" @close="reset" top="36vh">
      <el-form :model="resetForm">
        <el-form-item label="工号" label-width="80px">
          <el-input type="text" v-model="resetForm.jobNum" placeholder="请输入工号" autocomplete="off"
            maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="手机号" label-width="80px">
          <el-input type="text" v-model="resetForm.phone" placeholder="请输入手机号" autocomplete="off"
            maxlength="20"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleReset">重置密码</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      resetForm: {
        jobNum: '',
        phone: ''
      },
      loginModel: 'staff',
      dialogVisible: false,
    };
  },
  methods: {
    async handleLogin() {
      this.$store.dispatch('updateLoginModel', this.loginModel); // 更新 Vuex 中的 loginModel
      this.$store.dispatch('updateJobNum', this.loginForm.account); // 更新 Vuex 中的 loginModel
      try {
        const response = await axios.post('/auth/login',
          {
            account: this.loginForm.account,
            password: this.loginForm.password,
            loginModel: this.loginModel
          });
        if (response.status === 200 && response.data.success) {
          console.log('Login successful:', this.loginForm.account, this.loginForm.password);
          this.$message.success('登录成功！');
          localStorage.setItem('token', "your_token");
          
          this.$router.push('/home');
        } else {
          console.log('Login failed', this.loginForm.account, this.loginForm.password);
          this.$message.error('Login failed: Invalid account or password');
        }
      } catch (error) {
        console.log('Login failed', this.loginForm.account, this.loginForm.password);
        this.$message.error('Login failed: Invalid account or password');
      }
    },
    openDialog() {
      this.dialogVisible = true;
    },
    async handleReset() {
      try {
        const response = await axios.post('/auth/reset-password', {
          jobNum: this.resetForm.jobNum,
          phone: this.resetForm.phone
        });
        if (response.status === 200 && response.data) {
          this.$message.success('密码重置成功！');
          this.dialogVisible = false; // 关闭对话框
          this.reset();
          this.$router.push('/login');  // 跳转到登录页面
        } else {
          this.$message.error('重置密码失败!');
        }
      } catch (error) {
        this.$message.error('重置密码失败!');
      }
    },
    reset() {
      this.resetForm.jobNum = '';
      this.resetForm.phone = '';
    }
  },
};
</script>

<style scoped>
.login-container {
  width: 300px;
  margin: 100px auto;
}

.login-btn {
  margin-right: 20px;
  /* 设置按钮和链接之间的间距 */
}
</style>